package com.michaeliu.flutter.study.flutter_sample

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.TextView
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory
import org.w3c.dom.Text

class CustomPlatformViewFactory(private val binaryMessenger: BinaryMessenger) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {


    override fun create(context: Context?, viewId: Int, args: Any?): PlatformView {

        // 创建原生 View（这里以 TextView 为例）
        val textView = TextView(context).apply {
            text = "这是 Android 原生 TextView"
            textSize = 16f
            setTextColor(Color.WHITE)
        }

        val channel = MethodChannel(binaryMessenger, "com.michaeliu.flutter.study/native_view_$viewId")
        channel.setMethodCallHandler { call, result ->
            when(call.method) {
                "updateText" -> {
                    val newText = call.argument<String>("text") ?: ""
                    textView.text = newText
                    result.success(true)
                }
                else -> {
                    result.notImplemented()
                }
            }
        }


        return object : PlatformView {
            override fun getView(): View {
                return textView
            }

            override fun dispose() {
                //资源释放
            }
        }
    }
}